<template>
  <div class="editAddress">
    <van-nav-bar title="修改/删除地址" left-arrow @click-left="onClickLeft" />
    <van-address-edit
      :show-area="false"
      show-delete
      :tel-validator="validator"
      @save="onSave"
      @delete='onDelete'
      :address-info="{
          name:addets.aDsser,
          tel:addets.aPhone,
          addressDetail:addets.aDs
      }"
    />
  </div>
</template>
 <script>
export default {
  props: ["id"],
  data() {
    return {
        addets:{}
    };
  },
  methods: {
    //返回上一页
    onClickLeft() {
      this.$router.push("/ipam");
    },
    //手机号码验证
    validator(event) {
      // console.log(event);
      let myreg = /^[1][3-9][0-9]{9}$/;
      if (!myreg.test(event)) {
        // console.log("手机号错误");
        return false;
      } else {
        // console.log('手机号正确')
        return true;
      }
    },
    //点击保存按钮时触发
    onSave(val) {
      console.log(val);
      let id = this.$route.params.id;
      let data = `aDsser=${val.name}&aPhone= ${val.tel}&aDs= ${val.addressDetail}&aId=${id}`;
      this.axios.post(`/kc/addmod`, data).then((res) => {
        // console.log(res);
        //提示已成功添加
        this.$toast(`修改成功`);
        //跳转到地址管理主页面
        this.$router.push('/Ipam');
      });
    },
    loadAddress() {
      //获取地址管理页面传过来的参数
      let aid = this.$route.params.id;
      // console.log(aid);
      this.axios.get(`/kc/addonly?aId=${aid}`).then((res) => {
        this.addets=res.data.result[0];
        console.log(res.data.result[0]);
      });
    },
    //点击确认删除地址时触发
    onDelete() {
      let id = this.$route.params.id
      let data = `aId=${id}`;
      this.axios.post('/kc/addel',data).then(res=>{
        //   console.log(res);
        this.$toast('已删除');
        //跳转到地址管理主页面
        this.$router.push('/Ipam');
      })
    },
  },
  mounted() {
    this.loadAddress();
  },
};
</script>
<style lang="scss">
.editAddress {
  text-align: left;
  height: 100vh;
  .van-icon {
    color: #000;
  }
  .van-nav-bar__text {
    color: #666;
  }
  .van-button--danger {
    color: #000;
    background-color: #ffcb00;
    border: 1px solid #ffcb00;
  }
}
</style>